package com.smzd.framework.system.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.core.dao.AbstractDao;
import com.smzd.core.domain.Many2Many;
import com.smzd.core.mappers.IMapper;
import com.smzd.framework.system.domain.Role;
import com.smzd.framework.system.mappers.RoleMapper;
import com.smzd.framework.utils.db.RPage;

@Component
public class RoleDao extends AbstractDao<Role> {
    @Autowired
    private RoleMapper mapper;

    @Override
    protected IMapper<Role> getMapper() {
        return mapper;
    }

    public List<Role> getPrivilegeRolesByMeetingId(int meetingid) {
        return mapper.getPrivilegeRolesByMeetingId(meetingid);
    }

    public List<Role> getPrivilegeRolesByUserId(int userid) {
        return mapper.getPrivilegeRolesByUserId(userid);
    }

    public RPage<Role> getGroupRoleListByPage(RPage<Role> page) {
        preparePage(page);
        List<Role> elems = mapper.getGroupRoleListByPage(page);
        page.setElems(elems);
        int total = mapper.getGroupRoleListCountByPage(page);
        page.setTotalCount(total);
        completePage(page);
        return page;
    }

    public void saveGroupRoles(List<Many2Many> toAdd, List<Many2Many> toDel) {
        for (Many2Many add : toAdd) {
            mapper.insertGroupRole(add);
        }
        for (Many2Many del : toDel) {
            mapper.removeGroupRole(del);
            // 这个地方会触发一系列的连锁反应的
            disableGroupRoleReduce(del);
        }
    }

    private void disableGroupRoleReduce(Many2Many del) {
        List<Many2Many> groupRoles = mapper.getGroupRolesReduce(del);
        if (groupRoles != null && groupRoles.size() > 0) {
            for (Many2Many gr : groupRoles) {
                if (gr.isDisabled()) {
                    continue;
                }
                gr.setDisabled(true);
                mapper.updateGroupRoleDisabled(gr);
                disableGroupRoleReduce(gr);
            }
        }

    }

    /**
     * 获取所有模块中的功能所附着的角色
     * 
     * @return
     */
    public List<Role> getRolesInAllModules() {
        return mapper.getRolesInAllModules();
    }

    /**
     * 获取指定的权限组所配备的权限
     * 
     * @param id
     * @return
     */
    public RPage<Role> getRolesByGroup(RPage<Role> page) {
        preparePage(page);
        List<Role> roles = mapper.getRolesByGroup(page);
        page.setElems(roles);
        int totalCount = mapper.getRolesCountByGroup(page);
        page.setTotalCount(totalCount);
        completePage(page);
        return page;
    }

    /**
     * 获取对指定组可进行配置的角色权限。所有角色都可用，目标为page.id所指出的组。
     * 
     * @param page
     */
    public RPage<Role> getGroupRoleListByPageForMeeting0(RPage<Role> page) {
        preparePage(page);
        List<Role> roles = mapper.getGroupRolesForMeeting0(page);
        page.setElems(roles);
        int totalCount = mapper.getGroupRolesCountForMeeting0(page);
        page.setTotalCount(totalCount);
        completePage(page);
        return page;
    }

    /**
     * 获取对指定组可进行配置的角色权限。用户为指定Meeting(参数为page.param2)的Owner，参数为page.param1，
     * 因此角色份两部分组成，不会存在不能使用的角色，目标为page.id所指出的组。
     * 
     * @param page
     */
    public RPage<Role> getGroupRoleListByPageForOwner(RPage<Role> page) {
        preparePage(page);
        List<Role> roles = mapper.getGroupRolesForOwner(page);
        page.setElems(roles);
        int totalCount = mapper.getGroupRolesCountForOwner(page);
        page.setTotalCount(totalCount);
        completePage(page);
        return page;
    }

    /**
     * 获取对指定组可进行配置的角色权限。用户为普通管理员，为并非角色都可用，参数为page.param1，目标为page.id所指出的组。
     * 
     * @param page
     * @return
     */
    public RPage<Role> getGroupRoleListByPageForManager(RPage<Role> page) {
        preparePage(page);
        List<Role> roles = mapper.getGroupRolesForManager(page);
        page.setElems(roles);
        int totalCount = mapper.getGroupRolesCountForManager(page);
        page.setTotalCount(totalCount);
        completePage(page);
        return page;
    }

}
